﻿using System.ComponentModel;
using System.Windows;
using FU.Capstones.IMS.Web.Common;
using FU.Capstones.IMS.Web.Common.InventoryServiceProxy.DataContracts;
using FU.Capstones.IMS.Web.Common.InventoryServiceProxy.EventArgs;
using FU.Capstones.IMS.Web.Common.LoginHandlers;
using FU.Capstones.IMS.Web.Silverlight.Models;
using Navigation.Silverlight;

namespace FU.Capstones.IMS.Web.Silverlight.Popups
{
    public partial class LoginPopup : INotifyPropertyChanged
    {
        public LoginPopup()
        {
            InitializeComponent();
            Account = new Account()
                          {
                              Username = IMSIsolatedStorage.CurrentUser.Username,
                              Password = IMSIsolatedStorage.CurrentUser.Password
                          };
            RememberPassword = IMSIsolatedStorage.RememberPassword;
            DataContext = this;
            model.LoginCompleted += LoginCompleted;
            MessageVisibility = Visibility.Collapsed;
        }

        void LoginCompleted(object sender, SingleFeedbackCompletedEventArgs<Account> e)
        {
            IsBusy = false;
            if (e.Error!=null)
            {
                MessageVisibility = Visibility.Visible;
                Message = e.Error.Message;
            }
            else
            {
                if (e.Result.IsSuccessful)
                {
                    //Get the access rights from service
                    Account = e.Result.Data;
                    //Store login info in Isolated storage
                    IMSIsolatedStorage.RememberPassword = RememberPassword;
                    IMSIsolatedStorage.CurrentUser = Account;
                    //
                    //Subcribe to the MainPage
                    AccountPublisher.NotifyAccountChanged(Account);
                    //Navigate to default page
                    NavigationManager.NavigateTo(WebConstants.NavigationPages.HomePage);
                    DialogResult = true;
                }
                else
                {
                    MessageVisibility = Visibility.Visible;
                    Message = e.Result.Message;
                }   
            }
            
        }

        #region Binding Property

        private Account account;
        public Account Account
        {
            get{return account;}
            set
            {
                if (!ReferenceEquals(account, value))
                {
                    account = value;
                    RaisePropertyChanged("Account");
                }
            }
        }

        private bool rememberPassword;
        public bool RememberPassword
        {
            get { return rememberPassword; }
            set
            {
                if (rememberPassword != value)
                {
                    rememberPassword = value;
                    RaisePropertyChanged("RememberPassword");
                }
            }
        }
		
		
        private bool isBusy;
        public bool IsBusy
        {
            get { return isBusy; }
            set
            {
                if (isBusy != value)
                {
                    isBusy = value;
                    RaisePropertyChanged("IsBusy");
                }
            }
        }
		
		private string message;
        public string Message
        {
            get { return message; }
            set
            {
                if (message != value)
                {
                    message = value;
                    RaisePropertyChanged("Message");
                }
            }
        }
		
		private Visibility messageVisibility;
        public Visibility MessageVisibility
        {
            get { return messageVisibility; }
            set
            {
                if (messageVisibility != value)
                {
                    messageVisibility = value;
                    RaisePropertyChanged("MessageVisibility");
                }
            }
        }

        #endregion

        #region Fields

        LoginPopupModel model =  new LoginPopupModel();

        #endregion

        #region OnClicked

        private void DoLogin(object sender, RoutedEventArgs e)
        {
            Account.ValidateAll();
            if (!Account.HasErrors)
            {
                MessageVisibility = Visibility.Collapsed;
                //model.SetClientCredentials(account.Username, account.Password);
                model.LoginAsync(account);
                IsBusy = true;
            }
            else
            {
                Message = WebConstants.InvalidInputDataMessage;
                MessageVisibility = Visibility.Visible;
            }
        }

        private void DoCancel(object sender, RoutedEventArgs e)
        {
            Account = new Account();
            MessageVisibility = Visibility.Collapsed;
        }

        #endregion

        #region Implementation of INotifyPropertyChanged

        public event PropertyChangedEventHandler PropertyChanged;

        private void RaisePropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion
    }
}

